#!/usr/bin/python
# -*- coding: utf-8 -*-

"""
Author: czf, Sky
Module Description: item_utils ... 
File: item_utils.py
Date: 2019/1/3
"""


def format_item_list_to_dict(item_list):
    """
    将列表形式的物品转换为字典形式，供客户端识别
    :param item_list: list [[物品类型，物品id，数量],...]
    :return: list [{"ItemType": item_type, "ItemId": item_id, "Count": count},...]
    """
    return [{"ItemType": item[0], "ItemId": item[1], "Count": item[2]} for item in item_list]


def same_award_together(award_list):
    """
    合并奖励列表中，同种奖励
    :param award_list: [[type, id, count]]
    :return: [[type, id, count]]
    """
    new_award_list = list()
    # 有相同物品则合并数量
    for award in award_list:
        for i, (item_type, item_id, count) in enumerate(new_award_list):
            new_good_type, new_good_id, new_good_count = award
            if new_good_type == item_type and new_good_id == item_id:
                count += new_good_count
                new_award_list[i] = [item_type, item_id, count]
                break
        # 没有相同物品则添加到获得奖励列表
        else:
            new_award_list.append(award)
    return new_award_list


def same_award_type_together(award_list):
    """
    合并奖励列表中，同类型奖励
    :param award_list: [[type, count]]
    :return: [[type, count]]
    """
    new_award_list = list()
    # 有相同类型物品则合并数量
    for award in award_list:
        for i, (item_type, count) in enumerate(new_award_list):
            new_good_type, new_good_count = award
            if new_good_type == item_type:
                count += new_good_count
                new_award_list[i] = [item_type, count]
                break
        # 没有相同物品则添加到获得奖励列表
        else:
            new_award_list.append(award)
    return new_award_list


def check_award_format(award_list, list_nest_num=2):
    """
    检测奖励格式层数，默认是二层 [[type1, id1, count1], [type2, id2, count2]]
    :param award_list:
    :param list_nest_num: 列表嵌套数量（几维列表）
    :return: True/False
    """

    if not isinstance(award_list, list) or len(award_list) == 0:
        return False

    # 直接检测字段长度是否超过上限，每档奖励个数不做限制
    for index, awards in enumerate(award_list):
        if list_nest_num == 3:
            for a in awards:
                if not isinstance(a, list) or not len(a) == 3:
                    return False
        elif list_nest_num == 2:
            if not isinstance(awards, list) or not len(awards) == 3:
                return False
        else:
            return False
    return True
